home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 526-550 / disk_541 / steal / src / basic.c next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  297 lines

  1. /****************************************************************************
  2.  
  3.                 Basic.c
  4.  
  5.     This file defines the basic operations that are needed to print
  6.     a set of structures. It is called from the Print module.
  7.  
  8.                     Rick van Rein, October 30, 1990
  9.  
  10. ****************************************************************************/
  11.  
  12.  
  13.  
  14. #include <functions.h>
  15. #include <intuition/intuition.h>
  16. #include <intuition/intuitionbase.h>
  17. #include <stdio.h>
  18.  
  19. #define abs(x) (((x) >= 0)? (x): (-(x)))
  20.  
  21.  
  22. struct IntuitionBase *IntuitionBase;
  23.  
  24. extern struct Window *win;
  25.  
  26.  
  27. #define INTUITEXT    0
  28. #define BORDER        1
  29. #define BORDERCOORDS    2
  30. #define IMAGE        3
  31. #define IMAGEDATA    4
  32. #define GADGET        5
  33. #define BOOLINFO    6
  34. #define PROPINFO    7
  35. #define STRINGINFO    8
  36. #define STRINGBUF    9
  37. #define STRINGUNDO    10
  38. #define WINDOW        11
  39. #define PROPBUF        12
  40. #define MENU        13
  41. #define MENUITEM    14
  42. #define NEWSCREEN    15
  43. #define COLORMAP    16
  44. #define WINDOWARRAY    17
  45. #define MENUARRAY    18
  46.  
  47.  
  48. char *StructNames []=
  49.  { "itx", "bor", "xy", "img", "pic", "gad", "bin", "pin", "sin", "str", "undo", "nwin", "pbuf", "men", "mit", "nscr", "cols", "swin", "smen" };
  50.  
  51. int ctr [19];
  52.  
  53. static FILE *file;
  54.  
  55.  
  56.  
  57. /***** Print a requester if the required file exists, and find out iff appending is wanted (=RetVal): */
  58.  
  59. static struct IntuiText negtv=
  60.  {
  61.    0,1,JAM1,
  62.    7,3,
  63.    NULL,
  64.    (UBYTE *) "Cancel",
  65.    NULL
  66.  };
  67.  
  68. static struct IntuiText postv=
  69.  {
  70.    0,1,JAM1,
  71.    7,3,
  72.    NULL,
  73.    (UBYTE *) "OverWrite",
  74.    NULL
  75.  };
  76.  
  77. static struct IntuiText line3=
  78.  {
  79.    0,1,JAM1,
  80.    12,25,
  81.    NULL,
  82.    (UBYTE *) "Already exists.",
  83.    NULL
  84.  };
  85.  
  86. static struct IntuiText line2=
  87.  {
  88.    0,1,JAM1,
  89.    12,15,
  90.    NULL,
  91.    NULL,        /* Filename filled in by program */
  92.    &line3
  93.  };
  94.  
  95. static struct IntuiText line1=
  96.  {
  97.    0,1,JAM1,
  98.    12,5,
  99.    NULL,
  100.    (UBYTE *) "File",
  101.    &line2
  102.  };
  103.  
  104. static int AppendReq (fn)
  105.    char *fn;
  106.  {
  107.    line2.IText=(UBYTE *) fn;
  108.    return AutoRequest (win,&line1,&postv,&negtv,0L,0L,320L,72L);
  109.  }
  110.  
  111.  
  112.  
  113. /***** Print a formatted text in the way printf does it; No more than 4 args are expected: */
  114.  
  115. void mprintf (fmt,arg1,arg2,arg3,arg4)
  116.    char *fmt;
  117.    long arg1,arg2,arg3,arg4;            /* Should be enough */
  118.  {
  119.    static char buf [300]="";
  120.    int s=strlen (buf);
  121.    int newline,tabulate;
  122.  
  123.    sprintf (&buf [s],fmt,arg1,arg2,arg3,arg4);
  124.    while ((newline=FindChar (buf,'\n')) >= 0)
  125.     {
  126.       buf [newline]='\0';
  127.       tabulate=FindChar (buf,'\t');
  128.       if (tabulate>=0)
  129.          buf [tabulate]='\0';
  130.       fputs (buf,file);
  131.       if (tabulate>=0)
  132.        {
  133.          if (tabulate>48)
  134.         fputs ("\n\t\t\t\t\t\t",file);
  135.      else
  136.         /* How many tabs are needed?
  137.            We should end at x=48, so 6 tabs from left. If x is already >0, then we can
  138.            subtract one tab for every 8 characters. 7 is not enough! Round down!
  139.            So, the number of tabs is 6 - (reached_x >> 3). Note that reached_x is
  140.            already determined: It can be found in tabulate! */
  141.             fputs (&"\t\t\t\t\t\t" [tabulate>>3],file);
  142.      fputs (&buf [tabulate+1],file);
  143.        }
  144.       fputc ('\n',file);
  145.       strcpy (buf,&buf [newline+1]);        /* Overwrite old line; It's finished! */
  146.     }
  147.  }
  148.  
  149.  
  150.  
  151. /***** Open a file for output; TRUE is returned iff opening went OK: */
  152.  
  153. int OpenPrintFile (name)
  154.    char *name;
  155.  {
  156.    int cont;
  157.    struct Lock *lock;
  158.  
  159.    lock=Lock (name);        /* Test if it already is there */
  160.    if (lock)
  161.     {
  162.       UnLock (lock);
  163.       cont=AppendReq (name);    /* Ask if we want to OverWrite old file */
  164.     }
  165.    else
  166.       cont=1;
  167.  
  168.    if (cont)
  169.       file=fopen (name,"w");
  170.    else
  171.       file=NULL;
  172.  
  173.    if (!file)
  174.       return 0;            /* It didn't work out as it should */
  175.    else
  176.     {
  177.       mprintf ("/***** Intuition structures stolen by means of Steal 1.1 by Rick van Rein */\n\n\n"
  178.          "#include <intuition/intuition.h>\n"
  179.          "#include <graphics/view.h>\n\n"
  180.          "#define BLUE_0      0\n"
  181.          "#define WHITE_1     1\n"
  182.          "#define BLACK_2     2\n"
  183.          "#define ORANGE_3    3\n\n");
  184.  
  185.       return 1;            /* All went as it should */
  186.     }
  187.  }
  188.  
  189.  
  190. /***** Close the PrintFile; Do not send a last '\n' to empty the mprintf-buffer; This must be OK! */
  191.  
  192. void ClosePrintFile ()
  193.  {
  194.    fclose (file);
  195.  }
  196.  
  197.  
  198.  
  199. /***** Seek for a character in the goiven buffer; Return -1 if not found; Otherwise find-index: */
  200.  
  201. static int FindChar (buf,ch)
  202.    char *buf;
  203.    char ch;
  204.  {
  205.    int loc=0;
  206.    while (buf [loc] && buf [loc] != ch)
  207.       loc++;
  208.    return ((buf [loc]==ch)? loc: -1);
  209.  }
  210.  
  211.  
  212. /***** Print a string in a C-readable format: */
  213.  
  214. void PrintStr (str)
  215.    char *str;
  216.  {
  217.    mprintf ("\"");
  218.    while (*str)
  219.     {
  220.       if (*str<' ')
  221.          switch (*str)
  222.       {
  223.         case '\n':mprintf ("\\n");break;
  224.         case '\t':mprintf ("\\t");break;
  225.         default:  mprintf ("\%3o",*str);
  226.       }
  227.       else
  228.          if (*str!='\"')
  229.             mprintf ("%c",*str);
  230.      else
  231.         mprintf ("\\\"");
  232.       str++;
  233.     }
  234.    mprintf ("\"");
  235.  }
  236.  
  237.  
  238. /***** Print a Pen as if it was given in using several labels: */
  239.  
  240. void PrintPen (pen)
  241.    int pen;
  242.  {
  243.    static char *penarr [4] = { "BLUE_0","WHITE_1","BLACK_2","ORANGE_3" };
  244.    if (pen>=0 && pen<4)
  245.       mprintf ("%s",penarr [pen]);
  246.    else
  247.       mprintf ("%d",pen);
  248.  }
  249.  
  250.  
  251. /***** Print all the Flags given in the first parameter.
  252.        fields Gives an array with the fields that should be AND-ed with those flags one by one;
  253.        The array ends in a 0L. If the result of the AND equals the value in matches at the same
  254.        index, the entry in names, (at that same index again) will be printed: */
  255.  
  256. void PrintFlags (flags,fields,matches,names)
  257.    long flags;
  258.    long fields [],matches [];    /* Closed by 0L in fields */
  259.    char *names [];
  260.  {
  261.    int found=0;
  262.  
  263.    while (*fields)
  264.     {
  265.       if ((flags & *fields)==*matches)
  266.        {
  267.          mprintf ("%s%s",found? " | ": "",*names);
  268.      found=1;
  269.        }
  270.       fields++;
  271.       matches++;
  272.       names++;
  273.     }
  274.  
  275.    if (!found)
  276.       mprintf ("0");
  277.  }
  278.  
  279.  
  280. /***** Print a DrawMode as if it was given in using several labels: */
  281.  
  282. static long DrawModeFld []=
  283.  { JAM2, JAM2, COMPLEMENT, INVERSVID, 0L };
  284.  
  285. static long DrawMode []=
  286.  { JAM2, JAM1, COMPLEMENT, INVERSVID };
  287.  
  288. static char *DrawModeNms []=
  289.  { "JAM2", "JAM1", "COMPLEMENT", "INVERSVID" };
  290.  
  291.  
  292. void PrintDrawMode (md)
  293.    int md;
  294.  {
  295.    PrintFlags ((long) md,DrawModeFld,DrawMode,DrawModeNms);
  296.  }
  297.